import { App } from 'vue'

// 自定义指令

export default (app: App<Element>) => {
  // 注册全局指令，在1秒内不难重复点击某个元素
  app.directive('onceClick', (el, binding) => {
    el.addEventListener('click', () => {
      setTimeout(() => el.blur(), 500)

      if (binding.value != null && typeof binding.value !== 'number')
        return console.error('指令onceClick的值必须是数字')

      if (el.style.pointerEvents === '') {
        el.style.pointerEvents = 'none'
        setTimeout(() => {
          el.style.pointerEvents = ''
        }, binding.value || 1000)
      }
    })
  })

  //   无法选取
  app.directive('notChoose', (el, binding) => {
    if (typeof binding.value !== 'boolean')
      return console.error('指令notChoose的值必须是boolean类型')

    el.addEventListener('click', () => {
      if (binding.value !== true) el.blur()
    })

    if (binding.value === true) {
      el.style.pointerEvents = 'none'
      el.focus()
    } else {
      el.style.pointerEvents = ''
      el.blur()
    }
  })
}
